package com.fzj.charging.order.quartz;

import com.fzj.charging.order.dao.mapper.OrderSuccessMapper;
import com.fzj.charging.order.dao.repository.OrderRepository;
import com.fzj.charging.order.pojo.po.OrderFailPO;
import com.fzj.charging.order.pojo.po.OrderSuccessPO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.UUID;

@Slf4j
public class DeviceCheckJob implements Job {


    /**
     * 必须有无参构造器，否则抛异常 NoSuchMethodException
     */
    public DeviceCheckJob() {

    }

    public DeviceCheckJob(String orderNo, Integer gunId) {
        //创建调度器工厂
        StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();

        try {
            //获取调度器Scheduler实例
            Scheduler scheduler = schedulerFactory.getScheduler();

            //创建定时任务
            // 定义一个JobDetail，它包含了要执行的Job的类名等信息
            // Quartz 在实例化任务时会使用反射来调用这个构造器，因此需要有无参构造器
            JobDetail jobDetail = JobBuilder.newJob(DeviceCheckJob.class)
                    .withIdentity("device-check-job" + UUID.randomUUID(), "device-check-job-group")
                    .usingJobData("orderNo",orderNo)
                    .usingJobData("gunId",gunId)
                    .build();
            long now = System.currentTimeMillis();
            long deviceCheckTime = now + 60 * 1000;
            Date triggerTime = new Date(deviceCheckTime);
            log.debug("设备检查任务将在当前时间{}的一分钟后{}开始执行", new Date(now), triggerTime);

            //定义触发器
            Trigger trigger = TriggerBuilder.newTrigger()
                    .startAt(triggerTime)
                    .build();

            //调度器将任务和触发器进行绑定
            scheduler.scheduleJob(jobDetail, trigger);
            //启动调度器
            scheduler.start();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调度器调用这个方法执行具体业务
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.debug("设备自检任务在规定时间(一分钟后)执行");
        log.debug("查询数据库中记录，判断设备状态TODO......");
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String orderNo = jobDataMap.getString("orderNo");
        Integer gunId = jobDataMap.getInt("gunId");

        //OrderSuccessMapper orderSuccessMapper = SpringContextUtils.getBean(OrderSuccessMapper.class);
        OrderRepository orderRepository = SpringContextUtils.getBean(OrderRepository.class);
        OrderSuccessPO successOrder = orderRepository.getSuccessOrder(orderNo);
        if(successOrder==null){
            OrderFailPO failOrder = orderRepository.getFailOrder(orderNo);
            if(failOrder==null){
                OrderFailPO orderFailPO = new OrderFailPO();
                orderFailPO.setBillId(orderNo);
                orderFailPO.setGunId(gunId);
                orderFailPO.setFailDesc("设备自检失败-设备无响应");
                log.debug("设备自检失败-设备无响应,插入失败订单记录：{}",orderFailPO);
                Integer row = orderRepository.saveFailOrder(orderFailPO);
                log.debug("设备自检失败-设备无响应,插入失败订单记录：{},影响行数：{}",orderFailPO,row);
                log.debug("通知用户");
                log.debug("通知维修人员");
            }else{
                log.debug("有失败订单记录，设备有响应");
            }
        }else{
            log.debug("有成功订单记录，设备自检正常");
        }

    }
}
